package com.example.androidnote;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.androidnote.Util.ComplexAdapter;
import com.example.androidnote.Util.NoteDB;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private ListView lv;
    private Intent i;
    private ComplexAdapter adapter;
    private NoteDB noteDB;
    private SQLiteDatabase dbreader;
    private Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.button);
        lv = findViewById(R.id.items);
        noteDB = new NoteDB(this);
        dbreader =noteDB.getReadableDatabase();
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i=new Intent(MainActivity.this,AddContent.class);
                startActivity(i);
            }
        });
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cursor.moveToPosition(position);
                Intent i = new Intent
                        (MainActivity.this,DetailActivity.class);
                i.putExtra(NoteDB.ID,cursor.getInt
                        (cursor.getColumnIndex(NoteDB.ID)));
                i.putExtra(NoteDB.CONTENT,cursor.getString
                        (cursor.getColumnIndex(NoteDB.CONTENT)));
                i.putExtra(NoteDB.IMG_PATH,cursor.getString
                        (cursor.getColumnIndex(NoteDB.IMG_PATH)));
                i.putExtra(NoteDB.VED_PATH,cursor.getString
                        (cursor.getColumnIndex(NoteDB.VED_PATH)));
                startActivity(i);
            }
        });
    }
    public void selectDB(){
        cursor = dbreader.query(NoteDB.TABLE,null,null,null,null,null,null);
        adapter = new ComplexAdapter(this,cursor);
        lv.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectDB();
    }
}
